//
//  MyInfoViewController.m
//  PlayCatchGame
//
//  Created by 流言 on 2018/1/15.
//  Copyright © 2018年 清正. All rights reserved.
//

#import "MyInfoViewController.h"
#import "AppHeader.h"
#import "MyTableHeaderView.h"
#import "MyInfoTableCell.h"

#import "SetAppViewController.h"
#import "ServiceCenterVC.h"
#import "MyAssetInfoVC.h"
#import "PersonInfoVC.h"
#import "MyPrizeViewController.h"
#import "AdderssManageVC.h"
#import "InviteFriendVC.h"
#import "HelpViewController.h"

#import "UserAccountModel.h"

@interface MyInfoViewController () <UITableViewDelegate, UITableViewDataSource> {
    
    __weak IBOutlet NSLayoutConstraint *bgImage_topSpace;
    __weak IBOutlet NSLayoutConstraint *tableView_topSpace;
    BOOL isFirstIntoThisPage;
}

@property (strong, nonatomic) MyTableHeaderView *headerView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *dataArray;
@property (nonatomic, strong) UserAccountModel *accountModel; // 页面数据model

@end

@implementation MyInfoViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
//    if (@available(iOS11,*)) {
//
//    }
    
    isFirstIntoThisPage = YES;

    if (IS_IPHONE_X) {
        bgImage_topSpace.constant =-88;
    } else {
        
        bgImage_topSpace.constant =-64;
        
        if (@available(iOS 11.0, *)) {
            tableView_topSpace.constant = -64;

        } else {
            tableView_topSpace.constant =0;
        }
    }
    
    self.automaticallyAdjustsScrollViewInsets = NO;

    
    
    [self showNavigatioBarSeat:BaseBarItemDirectionRight withImage:[UIImage imageNamed:@"tixing"]];
    self.dataArray =@[@[@{@"title":@"我的奖品", @"imageName":@"jiangping"},
                        @{@"title":@"收货地址", @"imageName":@"dizhi"}],
                      @[@{@"title":@"邀请好友", @"imageName":@"yaoqinghaoyou"}],
                      @[@{@"title":@"游戏帮助", @"imageName":@"bangzhu"},
                        @{@"title":@"客服中心", @"imageName":@"kefu"},
                        @{@"title":@"设置", @"imageName":@"shezhi"}]].mutableCopy;
    self.tableView.tableHeaderView =self.headerView;
//    self.tableView.tableHeaderView.
    [TimerHandler performAfterDelay:0.01 handler    :^{
        [self.tableView reloadData];
    }];

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _headerView.nameLabel.text = [TheGlobalMethod getUserDefault:@"userName"];
    [_headerView.headButton sd_setImageWithURL:[NSURL URLWithString:[TheGlobalMethod getUserDefault:@"headImg"]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"touxiang"]];

//    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
//        [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    self.navigationController.navigationBar.translucent =YES;
    self.navigationController.navigationBar.barTintColor =[UIColor clearColor];
    
    [self _requestNetwork];

    if (@available(iOS 11.0, *)) {
        //        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        if (isFirstIntoThisPage) {
            self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 50, 0);
            isFirstIntoThisPage = NO;
        } else {
            self.tableView.contentInset = UIEdgeInsetsZero;
        }
  
    }
    

}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.translucent =NO;
    self.navigationController.navigationBar.barTintColor = APP_DEFAULT_COLOUR;
}

#pragma mark - Request Network
- (void)_requestNetwork {
    [HttpUrl GET:@"userAccount/selectMyAccount" dict:nil hud:self.view isShow:NO WithSuccessBlock:^(id data) {
        if (BB_isSuccess) {
            
            self.accountModel = [UserAccountModel mj_objectWithKeyValues:data[@"data"]];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                self.headerView.leftLabel.text = [NSString stringWithFormat:@"%ld", self.accountModel.allGold];
                self.headerView.centerLabel.text = [NSString stringWithFormat:@"%.2lf", self.accountModel.balanceMoney];
                self.headerView.rightLabel.text = [NSString stringWithFormat:@"%.2lf", self.accountModel.adProfitMoney];
            });
        } else {
            ShowAlertTitle(data[@"msg"]);
        } }];
}


- (MyTableHeaderView *)headerView {
    if (!_headerView) {

        _headerView =[MyTableHeaderView loadNib:@"MyTableHeaderView" frame:CGRectMake(0, 64, KScreenWidth, 210)];
        _headerView.isLogin =YES;
        _headerView.nameLabel.text = [TheGlobalMethod getUserDefault:@"userName"];
        [_headerView.headButton sd_setImageWithURL:[NSURL URLWithString:[TheGlobalMethod getUserDefault:@"headImg"]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"touxiang"]];
        [_headerView loginButtonClickBlock:^(UIButton *button) {
            [LoginViewController gotoLoginViewController:self loginType:^(BOOL isSuccess) {
                if (isSuccess) {
                    NSLog(@"登录成功");
                }
            }];
        }];
        
        [_headerView otherButtonBlock:^(UIButton *button) {
            if (button ==_headerView.headButton) {
                PersonInfoVC *personVC =[[PersonInfoVC alloc]init];
                personVC.title =@"个人资料";
                personVC.hidesBottomBarWhenPushed =YES;
                [self.navigationController pushViewController:personVC animated:YES];
            } else {
                NSInteger type =0;
                NSString *title =@"";
                if (button ==_headerView.leftButton) {
                    type =1;    title =@"金豆";
                } else if (button ==_headerView.centerButton) {
                    type =2;    title =@"现金红包";
                } else if (button ==_headerView.rightButton) {
                    type =3;    title =@"推广收益";
                }
                MyAssetInfoVC *assetVC =[[MyAssetInfoVC alloc]init];
                assetVC.title =title;
                assetVC.typeVC =type;
                assetVC.hidesBottomBarWhenPushed =YES;
                [self.navigationController pushViewController:assetVC animated:YES];
            }
        }];
    }
    return _headerView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *contentArray =self.dataArray[section];
    return contentArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    return 55;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 74;
    }
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.frame), 10)];
    headerView.backgroundColor =tableView.backgroundColor;
    return headerView;
}

- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footerView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.frame), 0.1)];
    footerView.backgroundColor =tableView.backgroundColor;
    return footerView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyInfoTableCell *cell =[tableView dequeueReusableCellWithIdentifier:@"MyInfoTableCell"];
    if (!cell) {
        cell =[MyInfoTableCell findCellWithIdentifier:@"MyInfoTableCell"];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
    }
    NSArray *contentArray =self.dataArray[indexPath.section];
    NSDictionary *contentDict =contentArray[indexPath.row];
    cell.myImageView.image =[UIImage imageNamed:contentDict[@"imageName"]];
    cell.titleLabel.text =contentDict[@"title"];

    UIRectCorner rectCorner =UIRectCornerBottomLeft|UIRectCornerBottomRight|UIRectCornerTopLeft|UIRectCornerTopRight;
    if (contentArray.count ==1) {
        [cell.cellBgView cornerRectWith:rectCorner cornerRadii:CGSizeMake(10, 10)];
    } else if (indexPath.row ==0 || indexPath.row == contentArray.count-1) {
        if (CGRectGetWidth(cell.frame) == CGRectGetWidth(tableView.frame)) {
            rectCorner =UIRectCornerTopLeft|UIRectCornerTopRight;
            if (indexPath.row == contentArray.count-1) {
                rectCorner =UIRectCornerBottomLeft|UIRectCornerBottomRight;
            }
            [cell.cellBgView cornerRectWith:rectCorner cornerRadii:CGSizeMake(10, 10)];
        }
    } else {
        [cell.cellBgView cornerRectWith:rectCorner cornerRadii:CGSizeMake(0, 0)];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section ==0 && indexPath.row ==0) {
        MyPrizeViewController *prizeVC =[[MyPrizeViewController alloc]init];
        prizeVC.title =@"我的奖品";
        prizeVC.hidesBottomBarWhenPushed =YES;
        [self.navigationController pushViewController:prizeVC animated:YES];
    } else if (indexPath.section ==0 && indexPath.row ==1) {
        AdderssManageVC *addressVC =[[AdderssManageVC alloc] init];
        addressVC.title =@"收货地址";
        addressVC.hidesBottomBarWhenPushed =YES;
        [self.navigationController pushViewController:addressVC animated:YES];
    } else if (indexPath.section ==1 && indexPath.row ==0) {
        InviteFriendVC *inviteVC =[[InviteFriendVC alloc] init];
        inviteVC.title =@"邀请好友";
        inviteVC.hidesBottomBarWhenPushed =YES;
        [self.navigationController pushViewController:inviteVC animated:YES];
    } else if (indexPath.section ==2 && indexPath.row ==0) {
        HelpViewController *helpVC =[[HelpViewController alloc] init];
        helpVC.title =@"游戏帮助";
        helpVC.hidesBottomBarWhenPushed =YES;
        [self.navigationController pushViewController:helpVC animated:YES];
    } else if (indexPath.section ==2 && indexPath.row ==1) {
        ServiceCenterVC *serviceVC =[[ServiceCenterVC alloc] init];
        serviceVC.title =@"客服中心";
        serviceVC.hidesBottomBarWhenPushed =YES;
        [self.navigationController pushViewController:serviceVC animated:YES];
    } else if (indexPath.section ==2 && indexPath.row ==2) {
        SetAppViewController *setVC =[[SetAppViewController alloc] init];
        setVC.title =@"设置";
        setVC.hidesBottomBarWhenPushed =YES;
        [self.navigationController pushViewController:setVC animated:YES];
    }
}
- (void)barItemRightButtonClick:(UIButton *)button {
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
